/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed.util;

import ejb.ApplicationFacade;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Danilo
 */
public class FileDownloadServlet extends HttpServlet{
    @EJB
    private ApplicationFacade af;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            int appId = 0;
            ServletOutputStream out = response.getOutputStream();
            if(request.getParameter("appId") != null  && request.getParameter("appId").isEmpty())
                return;
            appId = Integer.parseInt(request.getParameter("appId"));
            byte[] bytes = null;
            bytes = af.find(appId).getApkFile();
            if(bytes!=null){
                out.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();


        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
